package com.ctgu.elasticsearch.controller;

import com.ctgu.elasticsearch.entity.UserEntity;
import com.ctgu.elasticsearch.service.UserService;
import com.ctgu.elasticsearch.utils.ApiResult;
import jakarta.validation.Valid;
import org.hibernate.validator.constraints.ru.INN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @ ClassName UserController
 * @ Author Alex2
 * @ Date 2025/8/13 11:04
 **/
@RestController
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * @ Author: Alex
     * @ Description: 分词匹配
     */
    // http://localhost:8080/api/users/search?keyword=Alice
    @GetMapping("/search")
    public ApiResult searchUsers(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                 @RequestParam String keyword) throws IOException {
        return userService.searchUsers(pageNum, pageSize, keyword);
    }

    /**
     * @ Author: Alex
     * @ Description: 精确匹配-高亮查询
     */
    // http://localhost:8080/api/users/searchUsersHighLight?keyword=alice
    @GetMapping("/searchUsersHighLight")
    public ApiResult searchUsersHighLight(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                          @RequestParam(value = "keyword") String keyword) throws IOException {
        return userService.searchUsersHighLight(pageNum, pageSize, keyword);
    }

    /**
     * @ Author: Alex
     * @ Description: 插入用户，mysql-elasticsearch 同步
     */
    // http://localhost:8080/api/users/insertUser?username=Alex&password=888888&age=20
    @GetMapping("/insertUser")
    public ApiResult insertUser(@RequestParam("username") String username,
                                @RequestParam("password") String password,
                                @RequestParam("age") Integer age) throws IOException {
        return userService.insertUser(username, password, age);
    }

    /**
     * @ Author: Alex
     * @ Description: 更新用户，mysql-elasticsearch 同步
     */
    // http://localhost:8080/api/users/updateUser?id=1&username=Tom&password=666666&age=18
    @GetMapping("updateUser")
    public ApiResult updateUser(@RequestParam("id") Integer id,
                                @RequestParam("username") String username,
                                @RequestParam("password") String password,
                                @RequestParam("age") Integer age) throws IOException {
        return userService.updateUser(id, username, password, age);
    }

    /**
     * @ Author: Alex
     * @ Description: 删除用户，mysql-elasticsearch 同步
     */
    // http://localhost:8080/api/users/deleteUser?id=3
    @GetMapping("/deleteUser")
    public ApiResult deleteUser(@RequestParam(value = "id") Integer id) throws IOException {
        return userService.deleteUser(id);
    }
}